import pygame
from pygame import Rect
import time
import random
from gameRole import *

# 导入按键的检测
from pygame.locals import *

def gameover(screen,score): 
    background = pygame.image.load('resource/image/gameover_564x468.png').convert_alpha()
    score_img = myfont.render('score: '+str(score), True, (255,255,255))
    
    while 1:
        screen.fill((0,0,0))

        # 判断按键
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
        screen.blit(background,(230,22))
        screen.blit(score_img,(0,470))
        pygame.display.update()
        time.sleep(0.03)

if __name__ == '__main__':
	# pygame初始化
    pygame.init()

    # 载入效果音乐
    shoot_hotdog_sound = pygame.mixer.Sound('resource/sound/shoot_hotdog.wav')
    shoot_hotdog_sound.set_volume(0.1)
    dead_sound = pygame.mixer.Sound('resource/sound/mario_dead.wav')
    dead_sound.set_volume(0.1)
    hit_sound = pygame.mixer.Sound('resource/sound/hit_normal.wav')
    hit_sound.set_volume(0.3)
	# 载入游戏音乐并循环播放
    pygame.mixer.music.load('resource/sound/background.mp3')
    pygame.mixer.music.play(-1, 0)
    pygame.mixer.music.set_volume(0.7)

    # 加载字体，用于计分
    myfont = pygame.font.Font('resource/Stupido.ttf',40)

	# 创建窗口
    screen = pygame.display.set_mode((1024, 512))
	# 读出背景图片
    background = pygame.image.load('resource/image/background.png').convert()
    # 设置窗口标题
    pygame.display.set_caption("王校长大战僵尸")

    # 创建一个存放僵尸对象的列表
    zoombie_lst = []
    # 控制僵尸产生的频率
    zoombie_frequency = 0
    # 存储僵尸图像信息，用于生成不同类型僵尸
    zooombie_attr_dict = {1:(screen,1,17,115,140),2:(screen,2,17,115,140)}

    wxz = WXZ(screen)

    while 1:
        screen.blit(background,(0, 0))
        score_img = myfont.render(str(wxz.score), True, (105,105,105))
        screen.blit(score_img,(0,0))
        # 判断按键
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    shoot_hotdog_sound.play()
                    wxz.keyhandle('space')
        if event.type == KEYDOWN:
            if event.key == K_a or event.key == K_LEFT:
                wxz.keyhandle('left')
            elif event.key == K_d or event.key == K_RIGHT:
                wxz.keyhandle('right')
            elif event.key == K_w or event.key == K_UP:
                wxz.keyhandle('up')
            elif event.key == K_s or event.key == K_DOWN:
                wxz.keyhandle('down')                
                            
     
        # 每循环20次产生一个僵尸
        if zoombie_frequency % 20 == 0:
            i = random.choice((1,2))
            zoombie_lst.append(ZoomBie(*zooombie_attr_dict[i]))
        zoombie_frequency += 1
        if zoombie_frequency == 40:
            zoombie_frequency = 0
        for z in zoombie_lst:
            z.move()
            z.draw()
            # 如果僵尸对象走出了左边界，判定游戏结束
            if z.pos_x <= 0:
                dead_sound.play()
                pygame.mixer.music.stop()
                gameover(screen,wxz.score)
            # 如果走到这步，说明僵尸没出界，判断僵尸有没有摸到wxz
            wxz_center_x = wxz.pos_x + wxz.width//2 - 35
            wxz_center_y = wxz.pos_y + wxz.height//2
            z_center_x = z.pos_x + z.width//2
            z_center_y = z.pos_y + z.height//2 - 30
            if abs(wxz_center_x - z_center_x) <= (wxz.width//2 + z.width//2 ) and\
                abs(wxz_center_y - z_center_y) <= (wxz.height//2 + z.height//2 ):
                # 如果摸到，判定游戏结束
                dead_sound.play()
                pygame.mixer.music.stop()
                gameover(screen,wxz.score)
        
        wxz.draw()
        for h in wxz.hotdog_lst:
            h.move()
            h.draw()
            # 如果热狗对象飞出了边界，删除这个热狗
            if h.pos_x <= -52:
                wxz.hotdog_lst.remove(h)
            # 判断热狗是否击中王校长
            h_center_x = h.pos_x + h.width//2
            h_center_y = h.pos_y + h.height//2
            wxz_center_x = wxz.pos_x + wxz.width//2 - 35
            wxz_center_y = wxz.pos_y + wxz.height//2
            # 如果热狗击中wxz，判定游戏结束
            if abs(h_center_x - wxz_center_x) <= (h.width//2 + wxz.width//2)-20 and\
                abs(h_center_y - wxz_center_y) <= (h.height//2 + wxz.height//2)-10 and\
                h.passed_time >= 200:
                hit_sound.play()
                h.hit()
                dead_sound.play()
                pygame.mixer.music.stop()
                gameover(screen,wxz.score)

            # 遍历僵尸列表，判断是否击中僵尸
            for z in zoombie_lst:

                z_center_x = z.pos_x + z.width//2
                z_center_y = z.pos_y + z.height//2 - 30
                # 如果热狗击中僵尸
                if abs(h_center_x - z_center_x) <= (h.width//2 + z.width//2)-10 and\
                    abs(h_center_y - z_center_y) <= (h.height//2 + z.height//2)-10:
                    hit_sound.play()
                    h.hit()
                    # 击中一下加10分
                    wxz.score += 10
                    wxz.hotdog_lst.remove(h)

                    # 僵尸血量-1后判定是否死亡
                    z.live -= 1
                    if z.live == 0:
                        zoombie_lst.remove(z)

        pygame.display.update()
        time.sleep(0.03)


